<?php

namespace App\Http\Controllers\Plugin;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use AlibabaCloud\Client\AlibabaCloud;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage;
use Modules\Plugin\Libs\Voduploadsdk\Uploader\AliyunVodUploader;
use Modules\Plugin\Libs\Voduploadsdk\Uploader\UploadVideoRequest;
use AlibabaCloud\Vod\Vod;
//视频点播插件
class VodController extends Controller
{
    public $VOD_CLIENT_NAME = 'AliyunVodClientDemo';
    public $path;
    //以调用获取播放地址接口
    public function getPlayInfo($videoId)
    {
        $regionId = 'cn-shanghai';
        AlibabaCloud::accessKeyClient(config('sms.oss.accessKeyId'), config('sms.oss.accessKeySecret'))
            ->regionId($regionId)
            ->connectTimeout(1000)
            ->timeout(3000)
            ->name($this->VOD_CLIENT_NAME);
        return Vod::v20170321()
            ->getPlayInfo()
            ->client($this->VOD_CLIENT_NAME)
            ->withVideoId($videoId)    // 指定接口参数
            ->withAuthTimeout(3600 * 24)
            ->format('JSON')  // 指定返回格式
            ->request();      // 执行请求

    }
    //获取播放列表
    public function getVodList($videoId)
    {
        try{
            $playInfo = $this->getPlayInfo($videoId);
            return $playInfo->PlayInfoList->PlayInfo;
        }catch(Exception $e){
            info($e->getMessage());
        }
    }
    //上传视频
    public function uploadVideo(Request $request)
    {
        try{
            $vid = $this->getVideoPath($request);
            if($vid){
                Storage::delete($this->path);
                $data = [
                    'code' => 0,
                    'file' => $this->dealUploadVideo($vid),
                ];
                return response()->json($data);
            }
        }catch(Exception $e){
            info($e->getMessage());

        }
    }
    //处理视频地址
    public function dealUploadVideo($vid)
    {
        $data = $this->getVodList($vid);
        foreach($data as $val){
            if($val->Format == 'mp4'){
                $res['path'] = $val->PlayURL;
            }
        }
        $res['vid'] = $vid;
        return $res;
    }
    //获取到上传到阿里云地址
    public function getVideoPath($request)
    {
        $name = $this->getTitle($request);
        $path = $this->setVideoName($request, $name);
        try {
            $uploader = new AliyunVodUploader(config('sms.oss.accessKeyId'),  config('sms.oss.accessKeySecret'));
            $uploadVideoRequest = new UploadVideoRequest(public_path('storage/'.$path), $name);
            $uploadVideoRequest->setCateId(1000083424);
            //$uploadVideoRequest->setCoverURL("http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png");
            $uploadVideoRequest->setTags('家居,视频');
            //$uploadVideoRequest->setStorageLocation('outin-xx.oss-cn-beijing.aliyuncs.com');
            //$uploadVideoRequest->setTemplateGroupId('6ae347b0140181ad371d197ebe289326');
//            $userData = array(
//                "MessageCallback"=>array("CallbackURL"=>"https://demo.sample.com/ProcessMessageCallback"),
//                "Extend"=>array("localId"=>"xxx", "test"=>"www")
//            );
//            $uploadVideoRequest->setUserData(json_encode($userData));
            $res = $uploader->uploadLocalVideo($uploadVideoRequest);
            return $res;
        } catch (Exception $e) {
            info($e->getMessage());
        }
    }
    //获取后台视频存储地址
    public function setVideoName($request, $name)
    {
        $day = date('Y-m-d', time());
        $this->path = $request->file('file')->storeAs('video/'.$day, $name);
        return $this->path;
    }
    //获取视频名称
    public function getTitle($request)
    {
        $files = $request->file('file');
        $ext = $files->getClientOriginalExtension();
        $baseName = $request->filled('title') ? $request->input('title') : Str::random(6);
        return $baseName.'.'.$ext;
    }
}
